$(function(){

	topBottomTooler();
	if($.cookie('night')){
		openNight();
	}
	$('#content').css('font-family',$.cookie('font_family'));
	$('.con-font-family').val($.cookie('font_family'));
	$('#content').css('color',$.cookie('font_color'));
	$('.con-font-color').val($.cookie('font_color'));
	$('#content').css('font-size',$.cookie('font_size'));
	$('.con-font-size').val($.cookie('font_size'));
	$('.con-speed').val($.cookie('speed'));
	$('body').css('background',$.cookie('background'));
	$('.jumbotron').css('background',$.cookie('background'));
	$('.con-background').val($.cookie('background'));
	$('#content').css('width',$.cookie('width'));
	$('.con-width').val($.cookie('width'));

	$('#autopage').attr('checked',$.cookie('autopage'));
	$('#night').attr('checked',$.cookie('night'));

	if($.cookie('autoscroll')){
		scrollWindow();
	}

	$('.con-font-family').change(function(){
		var font_family = $(this).val();
		$.cookie('font_family',font_family);
		$('#content').css('font-family',font_family);
	});
	$('.con-font-color').change(function(){
		var font_color = $(this).val();
		$.cookie('font_color',font_color);
		$('#content').css('color',font_color);
	});

	$('.con-font-size').change(function(){
		var font_size = $(this).val();
		$.cookie('font_size',font_size);
		$('#content').css('font-size',font_size);
	});

	$('.con-speed').change(function(){
		var speed = $(this).val();
		$.cookie('speed',speed);
		var stop_scrool = scrollWindow(speed);
	});

	$('body').dblclick(function(){
		var speed = $('.con-speed').val();
		scrollWindow(speed);
	}).click(function(){
		if($.cookie('autoscroll')){
			$.cookie('autoscroll',null);
			clearTimeout($.cookie('stop_scrool'));
		}
	});

	$('.con-background').change(function(){
		var background = $(this).val();
		$.cookie('background',background);
		$('body').css('background',background);
		$('.jumbotron').css('background',background);
	});

	$('.con-width').change(function(){
		var width = $(this).val();
		$.cookie('width',width);
		$('#content').css('width',width);
	});

	$('#night').change(function(){
		if($(this).is(':checked')){
			openNight();
		}else{
			closeNight();
		}

	});

	$('#autopage').change(function(){
		if($(this).is(':checked')){
			$.cookie('autopage',true);
		}else{
			$.cookie('autopage',null);
		}
	});


	$(window).scroll(function(){
		if($('#autopage').is(':checked')){
			var b_t_f = isScrollBottom();
			if(b_t_f){
				location.href = $('#next-page').attr('href');
			}
		}
	});


   	//console.dir(parseInt($(window).scrollTop()));

   	//阅读记录
   	$(window).keydown(function(event){
   		keyboot = event.keyCode;
   		if(keyboot == 37){
   			location.href = $('#prev-page').attr('href');
   		}
   		if(keyboot == 39){
   			location.href = $('#next-page').attr('href');
   		}

   	});

});

//章节报错
function sendArticleError(book_id,article_id,message){
	var timestamp = Date.parse(new Date());
	var oldtime = $.cookie('send_error_time');
	if((timestamp - oldtime) <= 30000){
		swal('报送错误章节过于频繁',"请休息30秒后再执行此操作！","error");
		return false;
	}
	$.cookie('send_error_time',timestamp);
	//alert(timestamp - oldtime);
	if(article_id > 0 && book_id >0){
		$.ajax({
         type: "POST",
         url: articleError_url,
         data: "book_id="+book_id+"&article_id="+article_id+'&message='+message,
         success: function(msg){
            if(msg == 'success'){
              swal({
                   title:"报送错误章节成功",
                   text:"我们会在2分钟以前尽快处理！",
                   type:"success"
               });
            }else{
              swal('请稍后再试',"您可能之前举报过或者可能是网络不稳定","error");
            }

         }
      });
	}
}

function openNight(){
	$('body').css('background','#000');
	$('.jumbotron').css('background','#000');
	$('#content').css('color','#fff');
	$('h1').css('color','#fff');
	$('.con-font-color').val('');
	$('.con-background').val('');
	$.cookie('night',true);
	$.cookie('font_color',null);
	$.cookie('background',null);
}

function closeNight(){
	$('body').css('background','');
	$('.jumbotron').css('background','');
	$('#content').css('color','');
	$('h1').css('color','');
	$('.con-font-color').val('');
	$('.con-background').val('');
	$.cookie('night',null);
	$.cookie('font_color',null);
	$.cookie('background',null);
}

function isScrollBottom(){
	var dh = parseInt($('body').height())-791;
	var sh = parseInt($(window).scrollTop());
    if(sh >= dh){ //到达底部100px时,加载新内容
    	//alert(dh);alert(sh);
   		return true;
    }else{
    	return false;
    }
}

function scrollWindow(speed){
	if(speed <= 0){
		var speed = 2;
	}
	var speed = parseInt(speed)/2;
	var dh = parseInt($('body').height())-791;
	var sh = parseInt($(window).scrollTop());
	var scroll_Top_h = speed+sh;
	$.cookie('autoscroll',true);
	var stop_scrool=setInterval(function(){
		document.body.onmousewheel = function(event) {
			scroll_Top_h = parseInt($(window).scrollTop())+30;
		}
		scroll_Top_h = scroll_Top_h+speed;
		if(scroll_Top_h >= dh){
			clearTimeout(scroll_Top_h);
		}
		$('body').scrollTop(scroll_Top_h);

	},20);
	$.cookie('stop_scrool',stop_scrool);
}


